<template>
	<el-dialog title="人员考核" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-main class="nopadding" style="font-size: 16px">
				<div v-for="(item,index) in itemList" :key="index" style="margin: 15px">
					<div>
						{{ index + 1 }}、{{ item.title }}
					</div>
					<el-radio-group v-if="item.optType=='0'" v-model="itemList[index].selected">
						<el-radio v-for="(childItem,i) in item.childItem" :label="childItem.childId" :key="i">{{ childItem.childTitle }}/{{childItem.childScore}}分</el-radio>
					</el-radio-group>
					<el-checkbox-group v-if="item.optType=='1'" v-model="itemList[index].selected">
						<el-checkbox v-for="(childItem,i) in item.childItem" :label="childItem.childId" :key="i">{{ childItem.childTitle }}/{{childItem.childScore}}分</el-checkbox>
					</el-checkbox-group>
				</div>
			</el-main>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
export default {
	emits: ['success', 'closed'],
	components: {
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			apiObj: this.$API.file.attach.uploadFile,
			itemList:[],
			form: {
				planId:'',
				accountId:'',
			},
			resForm: {
				planId:'',
				accountId:'',
				scoreList:"",
				score:0
			},
			rules: {
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open() {
			this.visible = true;
			return this
		},
		submit() {
			this.$confirm('考核完毕后不可修改, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async() => {
				let resJson=[];
				this.itemList.forEach(item=>{
					let json = {
						itemId:item.itemId,
						selected:Object.prototype.toString.call(item.selected)=='[object Number]'?item.selected:item.selected.join(","),
					}
					resJson.push(json);
				})
				this.resForm.score= this.getResByItem();
				this.resForm.scoreList = JSON.stringify(resJson);
				this.isSaveing = true;
				let res = await this.$API.hr.hrKpiPlan.setScoreToUser.post(this.resForm);
				this.isSaveing = false;
				if (res.code == 200) {
					this.$emit('success')
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {
			});
		},
		getResByItem(){
			let score=0;
			this.itemList.forEach(item=>{
				let selected = item.selected;
				item.childItem.forEach(tempItem=>{
					if(tempItem.childId==selected)
					{
						score = score+Number(tempItem.childScore);
					}
				})
			})
			return score;
		},
		setData(data) {
			this.form.planId = data.planId;
			this.resForm.planId = data.planId;
			this.form.accountId = data.accountId;
			this.resForm.accountId = data.accountId;
			this.getHrKpiRecordForUser();
		},
		async getHrKpiRecordForUser(){
			let res = await this.$API.hr.hrKpiPlan.getHrKpiRecordForUser.get(this.form);
			if (res.code == 200) {
				this.itemList = JSON.parse(res.data.itemJson);
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
